package cn.com.controller;

import cn.com.beans.*;
import cn.com.service.*;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.annotation.Resource;
import javax.lang.model.SourceVersion;
import java.util.List;

@Controller
@RequestMapping("/search")
public class SearchController {

    @Resource
    private CollegeInfoService collegeInfoService;
    @Resource
    private MajorInfoService majorInfoService;
    @Resource
    private ProfessionInfoService professionInfoService;
    @Resource
    private MajorTypeService majorTypeService;
    @Resource
    private ProfessionTypeService professionTypeService;

    @GetMapping
    public String search(String keyword, String type, Model model) {
        String word  = keyword;
        System.out.println("type = " + type);
        System.out.println("word = " + word);
        model.addAttribute("word", word);
        if(type.equals("1")) {
            CollegeInfo collegeInfo = new CollegeInfo();
            System.out.println(keyword);
            collegeInfo.setCoName(keyword);
            List<CollegeInfo> collegeInfoList = collegeInfoService.fuzzyQueryAll(collegeInfo);
            Integer num = collegeInfoList.size() > 10 ? 10 : collegeInfoList.size();
            model.addAttribute("collegeList",collegeInfoList);
            model.addAttribute("number", num);
            return "search/searchcollege";
        } else if(type.equals("2")){
            MajorInfo majorInfo = new MajorInfo();
            majorInfo.setMaName(keyword);
            List<MajorType> majorTypeList = majorTypeService.findAll(null);
            List<MajorInfo> majorInfoList = majorInfoService.fuzzyQueryAll(majorInfo);
            Integer num = majorInfoList.size() > 10 ? 10 : majorInfoList.size();
            model.addAttribute("majorList", majorInfoList);
            model.addAttribute("typelist", majorTypeList);
            model.addAttribute("number", num);
            return "search/searchmajor";
        }
        ProfessionInfo professionInfo = new ProfessionInfo();
        professionInfo.setPrName(keyword);
        List<ProfessionType> professionTypeList = professionTypeService.findAll(null);
        List<ProfessionInfo> professionInfoList = professionInfoService.fuzzyQueryAll(professionInfo);
        Integer num = professionInfoList.size() > 10 ? 10 : professionInfoList.size();
        model.addAttribute("typeList", professionTypeList);
        model.addAttribute("professionList", professionInfoList);
        model.addAttribute("number", num);
        return "search/searchprofession";
    }
}
